<?php
/*
 * Created on 12.05.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

    function Utf8Win($str,$type="w")
    {
        static $conv='';
        if (!is_array($conv))
        {
            $conv = array();
            for($x=128;$x<=143;$x++)
            {
                $conv['u'][]=chr(209).chr($x);
                $conv['w'][]=chr($x+112);
            }
            for($x=144;$x<=191;$x++)
            {
                $conv['u'][]=chr(208).chr($x);
                $conv['w'][]=chr($x+48);
            }
            $conv['u'][]=chr(208).chr(129); // Ё
            $conv['w'][]=chr(168);
            $conv['u'][]=chr(209).chr(145); // ё
            $conv['w'][]=chr(184);
            $conv['u'][]=chr(208).chr(135); // Ї
            $conv['w'][]=chr(175);
            $conv['u'][]=chr(209).chr(151); // ї
            $conv['w'][]=chr(191);
            $conv['u'][]=chr(208).chr(134); // І
            $conv['w'][]=chr(178);
            $conv['u'][]=chr(209).chr(150); // і
            $conv['w'][]=chr(179);
            $conv['u'][]=chr(210).chr(144); // Ґ
            $conv['w'][]=chr(165);
            $conv['u'][]=chr(210).chr(145); // ґ
            $conv['w'][]=chr(180);
            $conv['u'][]=chr(208).chr(132); // Є
            $conv['w'][]=chr(170);
            $conv['u'][]=chr(209).chr(148); // є
            $conv['w'][]=chr(186);
            $conv['u'][]=chr(226).chr(132).chr(150); // №
            $conv['w'][]=chr(185);
        }
        if ($type == 'w') { return str_replace($conv['u'],$conv['w'],$str); }
        elseif ($type == 'u') { return str_replace($conv['w'], $conv['u'],$str); }
        else { return $str; }
    }
    
 function Encode ( $str, $type = 'w' )
{
 // $type:
// 'w' - encodes from UTF to win
 // 'u' - encodes from win to UTF

    static $conv='';
    if (!is_array ( $conv ))
    {   
        $conv=array ();
        for ( $x=128; $x <=143; $x++ )
        {
          $conv['utf'][]=chr(209).chr($x);
          $conv['win'][]=chr($x+112);
        }

        for ( $x=144; $x <=191; $x++ )
        {
               $conv['utf'][]=chr(208).chr($x);
               $conv['win'][]=chr($x+48);
        }
 
        $conv['utf'][]=chr(208).chr(129);
        $conv['win'][]=chr(168);
        $conv['utf'][]=chr(209).chr(145);
        $conv['win'][]=chr(184);
     }
     if ( $type=='w' )
          return str_replace ( $conv['utf'], $conv['win'], $str );
     elseif ( $type=='u' )
          return str_replace ( $conv['win'], $conv['utf'], $str );
     else
        return $str;
  }
  
function utf8_2_win1251 ($str_src)
{
    $str_dst = "";
    $i      = 0;
    while ($i<strlen($str_src))
    {
        $code_dst  = 0;
        $code_src1 = ord($str_src[$i]);
        $i++;

        if ($code_src1<=127)
        {
            $str_dst .= chr($code_src1);
            continue;
        }
        else
        if (($code_src1 & 0xE0) == 0xC0)
        {
            $code_src2 = ord($str_src[$i++]);
            if (($code_src2 & 0xC0) != 0x80)
                continue;

            $code_dst = ( ($code_src1 & 0x1F) << 6) + ($code_src2 & 0x3F);
        }
        else
        if (($code_src1 & 0xF0) == 0xE0)
        {
            $code_src2 = ord($str_src[$i++]);
            if (($code_src2 & 0xC0) != 0x80)
                continue;

            $code_src3 = ord($str_src[$i++]);
            if (($code_src3 & 0xC0) != 0x80)
                continue;

            $code_dst = ( ($code_src1 & 0x1F) << 12) + ( ($code_src2 & 0x3F) << 6) + ($code_src3 & 0x3F);
        }
        else
        if (($code_src1 & 0xF8) == 0xF0)
        {
            $code_src2 = ord($str_src[$i++]);
            if (($code_src2 & 0xC0) != 0x80)
                continue;

            $code_src3 = ord($str_src[$i++]);
            if (($code_src3 & 0xC0) != 0x80)
                continue;

            $code_src4 = ord($str_src[$i++]);
            if (($code_src4 & 0xC0) != 0x80)
                continue;

            $code_dst = ( ($code_src1 & 0x1F) << 18) + ( ($code_src2 & 0x3F) << 12) + ( ($code_src3 & 0x3F) << 6) + ($code_src4 & 0x3F);
        }
        else
        {
            continue;
        }
       
        if ($code_dst)
        {
            if ($code_dst==0x401)
            {
                $str_dst .= "�";
            }
            else
            if ($code_dst==0x451)
            {
                $str_dst .= "�";
            }
            else
            if ( ($code_dst>=0x410) && ($code_dst<=0x44F) )
            {
                $str_dst .= chr ($code_dst-848);
            }
            else
                $str_dst .= "&#{$code_dst};";
        }
    }
    return $str_dst;
}      
?>
